package com.gjm.ai.controller;

import com.gjm.ai.objectdata.Recipe;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
public class ChatClientController {

    @Autowired
    private ChatClient chatClient;

//    public ChatClientController(ChatClient.Builder chatClientBuilder) {
//        this.chatClient = chatClientBuilder
//                .defaultSystem("你是一个大学生，可以为我解决学习和生活上的问题")
//                .build();
//    }

    @GetMapping("/ai")
    public String generation(String userInput) {
        return this.chatClient.prompt()
                .user(userInput)
                .call()
                .content();
    }

    @GetMapping("/dish")
    public String dish(String userInput) {
        Recipe recipe = chatClient.prompt()
                .user(String.format("给出%s的食谱", userInput))
                .call()
                .entity(Recipe.class);

        return recipe.toString();
    }

    @GetMapping(value = "/stream", produces = "text/html;charset=utf-8")
    public Flux<String> stream(String userInput) {
        return this.chatClient.prompt()
                .user(userInput)
                .stream()
                .content();
    }
}
